package ru.incoremedia.gwttest.core.client.component;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;


public class FileUploader extends Composite {

    private FormPanel formPanel;

    public static enum UPLOAD_STATUS {
        OK, SIZE_LIMIT_EXCEPTION, SECURITY_EXCEPTION, FILE_FORMAT_EXCEPTION, UNKNOWN_EXCEPTION;
    }

    private HorizontalPanel horizontalPanel;

    private FileUpload fileUpload;

    public FileUploader(String servletPath, HasClickHandlers submit) {
        formPanel = new FormPanel();
        formPanel.setAction(servletPath);
        formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
        formPanel.setMethod(FormPanel.METHOD_POST);

        initWidget(formPanel);

        horizontalPanel = new HorizontalPanel();
        formPanel.setWidget(horizontalPanel);

        fileUpload = new FileUpload();
        fileUpload.setName("avatarUploadServlet");        

        horizontalPanel.add(fileUpload);

        horizontalPanel.add((Widget)submit);
        
        submit.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                formPanel.submit();
            }
        });
    }

    public FileUpload getFileUpload() {
        return fileUpload;
    }

    public FormPanel getFormPanel() {
        return formPanel;
    }

    public HorizontalPanel getHorizontalPanel() {
        return horizontalPanel;
    }
}
